package com.xuexiang.xuidemo.provider;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.XulPopupItem;
import com.xuexiang.xuidemo.util.TextUtils;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

/**
 * 适配器
 */
public class XulPopListProvider extends BaseItemProvider {
    private Context mContext;
    private List<XulPopupItem> mItems;

    public XulPopListProvider(Context context, List<XulPopupItem> mItems){
         this.mContext=context;
         this.mItems=mItems;
    }
    @Override
    public int getCount() {
        return mItems==null?0:mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems != null && position >= 0 && position < mItems.size()?mItems.get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final  Component component1;
          if(component==null){
              component1= LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_xui_popup_item,null,false);
          }else{
              component1=component;
          }
          XulPopupItem item=mItems.get(position);
          Text tv_Msg=(Text)component1.findComponentById(ResourceTable.Id_tv_Msg);
          if(!TextUtils.isEmpty(item.getItem())){
              tv_Msg.setText(item.getItem());
          }
        return component1;
    }
}
